package com.zdf.license.verify.service;

import com.zdf.license.core.content.LicenseContents;
import com.zdf.license.core.model.LicenseExtraParam;
import com.zdf.license.core.model.LicenseResult;
import com.zdf.license.core.service.AServerInfos;
import com.zdf.license.core.utils.Base64Utils;
import com.zdf.license.verify.config.LicenseVerifyProperties;
import com.zdf.license.verify.listener.ACustomVerifyListener;
import com.zdf.license.verify.listener.ParamType;
import com.zdf.license.verify.model.LicenseAuthInfo;
import com.zdf.license.verify.model.LicenseVerifyManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@AllArgsConstructor
public class LicenseVerifyService {

    private final LicenseVerifyProperties properties;

    /**
     * 获取申请码与授权信息
     * @return 申请码
     */
    public LicenseAuthInfo licenseAuthInfo() {
        // 申请码
        LicenseExtraParam extraParam = AServerInfos.getServer().getServerInfos(null);
        extraParam.setParams(getAppParams());
        String encode = Base64Utils.encodeLicenseExtraParam(extraParam);
        // 证书中携带的个性信息
        Map<String, Object> result = null;
        if (LicenseContents.PARAMS != null) {
            result = new HashMap<>();
            for (ACustomVerifyListener<?> customVerifyListener:ACustomVerifyListener.getCustomListenerList()) {
                ParamType paramType = customVerifyListener.paramType();
                result.put(paramType.getName(), LicenseContents.PARAMS.get(paramType.getKey()));
            }
        }
        return LicenseAuthInfo.builder()
                .code(encode)
                .beginTime(LicenseContents.BEGIN_TIME) // 证书开始时间
                .afterTime(LicenseContents.AFTER_TIME) // 证书结束时间
                .params(result) // 证书中包含的其他数据
                .build();
    }

    /**
     * 更新证书
     *
     * @param inputStream 证书
     * @throws Exception 更新失败
     */
    public void updateLicense(InputStream inputStream) throws Exception {
        try (FileOutputStream outputStream = new FileOutputStream(properties.getLicensePath())) {
            // 3.流的拷贝
            IOUtils.copy(inputStream, outputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
        LicenseResult licenseResult = LicenseVerifyManager.simpleInstall(properties.getVerifyParam());
        if (!licenseResult.getResult()) {
            throw new Exception("更新失败，请检查证书的合法性" + licenseResult.getMessage());
        }
    }

    /**
     * 获取所有认证参数
     */
    private Map<String, Object> getAppParams(){
        List<ACustomVerifyListener<?>> list = ACustomVerifyListener.getCustomListenerList();
        Map<String, Object> params = new HashMap<>();
        for (ACustomVerifyListener<?> aCustomVerifyListener:list) {
            params.put(aCustomVerifyListener.paramType().getKey(), aCustomVerifyListener.paramType().getValueType());
        }
        return params;
    }

}
